package com.ruoyi.algorithm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.algorithm.entity.OperatorVersion;
import com.ruoyi.algorithm.repository.ImageRepository;
import com.ruoyi.algorithm.repository.ImageVersionRepository;
import com.ruoyi.algorithm.repository.OperatorVersionRepository;
import com.ruoyi.algorithm.service.OperatorVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：yanfa-03
 * @date ：Created in 2021/7/13/10:14
 * @description ：
 * @version: 1.0
 */
@Service
public class OperatorVersionServiceImpl implements OperatorVersionService {

    @Autowired
    OperatorVersionRepository operatorVersionRepository;
    @Autowired
    private ImageRepository imageRepository;
    @Autowired
    private ImageVersionRepository imageVersionRepository;

    /**
     * 根据算子id查询算子版本列表
     *
     * @param operatorId 算子id
     * @return 算子版本列表
     */
    @Override
    public List<OperatorVersion> findByOperatorId(Long operatorId) {
        return operatorVersionRepository.listByOperatorId(operatorId);
    }

    /**
     * 新增算子版本
     *
     * @param operatorVersion 算子版本实体
     */
    @Override
    public void addOperatorVersion(OperatorVersion operatorVersion) throws RuntimeException {
        //判断如果算子id与算子版本version重复则不新增
        OperatorVersion one = operatorVersionRepository.getOne(
                Wrappers.lambdaQuery(OperatorVersion.class)
                        .eq(OperatorVersion::getVersion, operatorVersion.getVersion())
                        .eq(OperatorVersion::getOperatorId, operatorVersion.getOperatorId())
                        .eq(OperatorVersion::getDelFlag, false)
        );
        if (ObjectUtils.isNotEmpty(one)) {
            throw new RuntimeException("该版本已存在，请编辑或删除原版本数据");
        }
        operatorVersionRepository.insertOperatorVersion(operatorVersion);
    }

    /**
     * 编辑算子版本
     *
     * @param operatorVersion 算子版本
     * @return 更新成功与否
     */
    @Override
    public Boolean editOperatorVersion(OperatorVersion operatorVersion) {
        operatorVersion.setCreateTime(null);
        operatorVersion.setDelFlag(null);
        operatorVersion.setCreateUser(null);
        return operatorVersionRepository.updateByEntity(operatorVersion);
    }

    /**
     * 根据主键id删除
     *
     * @param operatorVersionId opVID主键
     * @return 删除成功与否
     */
    @Override
    public Boolean deleteById(Long operatorVersionId) {
        //查询是否已被删除
        OperatorVersion one = operatorVersionRepository.getOne(Wrappers.lambdaQuery(OperatorVersion.class)
                .eq(OperatorVersion::getId, operatorVersionId));
        if (one.getDelFlag()) {
            return false;
        }
        return operatorVersionRepository.updateById(new OperatorVersion(operatorVersionId, true));
    }

    /**
     * 根据id查找
     *
     * @param id 主键
     * @return 实例
     */
    @Override
    public OperatorVersion findByid(Long id) {
        OperatorVersion operatorVersion = operatorVersionRepository.getById(id);
        if (ObjectUtils.isNotEmpty(operatorVersion)) {
            if (operatorVersion.getDelFlag()) {
                return null;
            } else return operatorVersion;
        }
        return null;
    }


    /**
     * 该版本是否存在
     *
     * @param operatorId 算子id
     * @param version 版本
     * @return 是否存在
     */
    @Override
    public boolean isVersionExist(Long operatorId, String version) {
        List<OperatorVersion> list = operatorVersionRepository.list(Wrappers.lambdaQuery(OperatorVersion.class)
                .eq(OperatorVersion::getOperatorId, operatorId)
                .eq(OperatorVersion::getVersion, version)
                .eq(OperatorVersion::getDelFlag, false));
        return ObjectUtils.isEmpty(list);
    }

    /**
     * 根据算子id与版本号查询算子版本
     *
     * @param operatorId 算子id
     * @param version 版本号
     * @return 算子版本实体
     */
    @Override
    public OperatorVersion findbyVersion(Long operatorId, String version) {
        return operatorVersionRepository.getOne(Wrappers.lambdaQuery(OperatorVersion.class)
                        .eq(OperatorVersion::getOperatorId, operatorId)
                        .eq(OperatorVersion::getVersion, version)
                        .eq(OperatorVersion::getDelFlag, false),
                false);
    }

    /**
     * 查找已删除的版本列表
     *
     * @return 已删除的版本列表
     */
    @Override
    public List<OperatorVersion> findAlreadyDel() {
        return operatorVersionRepository.list(Wrappers.lambdaQuery(OperatorVersion.class)
                .eq(OperatorVersion::getDelFlag, true));
    }

    /**
     * 恢复已删除算子版本
     *
     * @param id
     */
    @Override
    public void recoverByid(Long id) {
        OperatorVersion one = operatorVersionRepository.getOne(Wrappers.lambdaQuery(OperatorVersion.class).eq(OperatorVersion::getId, id));
        if (ObjectUtils.isEmpty(one)) {
            throw new RuntimeException("没有找到该版本数据");
        }
        if (!one.getDelFlag()) {
            throw new RuntimeException("该数据未被删除");
        }
        String version = one.getVersion();
        Long operatorId = one.getOperatorId();
        OperatorVersion operatorVersion = operatorVersionRepository.getOne(Wrappers.lambdaQuery(OperatorVersion.class)
                .eq(OperatorVersion::getOperatorId, operatorId)
                .eq(OperatorVersion::getVersion, version)
                .eq(OperatorVersion::getDelFlag, false));
        if (ObjectUtils.isNotEmpty(operatorVersion)) {
            throw new RuntimeException("该版本数据已有重复");
        }
        operatorVersionRepository.updateById(new OperatorVersion(id, false));
    }
}
